package com.cskaoyan.catmall.realm;

import com.cskaoyan.catmall.dao.MarketUserMapper;
import com.cskaoyan.catmall.model.pojo.MarketUser;
import com.cskaoyan.catmall.model.pojo.MarketUserExample;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 创建日期: 2022-01-11-21-42
 * author : LL
 * description:
 */
@Component(value = "wxUserRealm")
public class WxuserRealm extends AuthorizingRealm {
    @Autowired
    MarketUserMapper userMapper;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        String principal = (String) authenticationToken.getPrincipal();
        MarketUserExample userExample = new MarketUserExample();
        MarketUserExample.Criteria criteria = userExample.createCriteria();
        criteria.andUsernameEqualTo(token.getUsername());
        criteria.andDeletedEqualTo(false);
        List<MarketUser> user = userMapper.selectByExample(userExample);
        if (user != null && user.size() != 0){
            return new SimpleAuthenticationInfo(user.get(0),user.get(0).getPassword(),getName());
        }
        return null;
    }
}